package cn.xie.domain.strategy.service.rule.factory;

import cn.xie.domain.strategy.model.entity.RuleActionEntity;
import cn.xie.domain.strategy.service.annotation.LogicStrategy;
import cn.xie.domain.strategy.service.rule.IRuleFiller;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: xiaoxie
 * create: 2025-08-08 17:05
 * @BelongsProject: bit-market
 * @BelongsPackage: cn.xie.domain.strategy.service.rule.factory
 * description: 默认规则工厂
 */
@Component
public class DefaultRuleFactory {
    Map<String, IRuleFiller<?>> ruleFillerMap = new ConcurrentHashMap<>();
    public DefaultRuleFactory(List<IRuleFiller<?>> ruleFillerLists) {
        this.ruleFillerMap = ruleFillerLists.stream()
                .filter(f -> AnnotationUtils.findAnnotation(f.getClass(), LogicStrategy.class) != null)
                .collect(Collectors.toMap(
                        f -> Objects.requireNonNull(AnnotationUtils.findAnnotation(f.getClass(), LogicStrategy.class))
                                .ruleModel().getCode(),
                        Function.identity()));
    }


    public <T extends RuleActionEntity.RaffleRuleEntity> Map<String, IRuleFiller<T>> openLogicFilter() {
        return (Map<String, IRuleFiller<T>>) (Map<?, ?>) ruleFillerMap;
    }


    @Getter
    @AllArgsConstructor
    public enum RuleModel{
        RULE_BLACK_LIST("rule_blacklist","抽奖前要过滤黑名单规则的规则模板"),
        RULE_WEIGHT("rule_weight","抽奖前要过滤规则权重的规则模板"),
        ;
        public final String code;
        public final String info;
    }

}
